﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

/// <summary>
/// banner广告出现后，场景自动压缩高度
/// 该脚本需要拖到Camera上面
/// </summary>
[DisallowMultipleComponent,RequireComponent(typeof(Camera))]
public class CameraBannerAdjust : MonoBehaviour {

	public bool debug = false;
	private Camera m_Camera;
	public Camera cam{ get{ return m_Camera; }}
	private float m_HeightScale = 1f;
	private float m_CurrHeightScale=1f;
	private float m_OffsetY;

	private static RectTransform _Bottom;
	private static List<CameraBannerAdjust> _instances = new List<CameraBannerAdjust>();

	// Use this for initialization
	void Start () {
		if(debug || Application.platform == RuntimePlatform.Android || Application.platform== RuntimePlatform.IPhonePlayer)
		{
			m_Camera = GetComponent<Camera>();
			_instances.Add(this);
		}
	}


	// Update is called once per frame
	void Update () 
	{
		if(m_Camera!=null){

			UpdateHeightScaleByAdHeight();

			if(m_CurrHeightScale!=m_HeightScale)
			{
				m_CurrHeightScale = Mathf.Lerp(m_CurrHeightScale,m_HeightScale,Time.deltaTime*3f);
				UpdateCameraMatrix();
			}
		}
	}

	void UpdateHeightScaleByAdHeight(){
		if(PluginManager.Instance.CheckAdIsShowed(AdType.AdTypeBannerAds)){
			if(m_HeightScale==1f){
				m_HeightScale = 1f-PluginManager.Instance.adsPlugin.GetBannerSize().y/Screen.height;
			}
		}else{
			m_HeightScale = 1f;
			#if UNITY_EDITOR
			if(debug) m_HeightScale = 0.9f;
			#endif
		}
	}

	void UpdateCameraMatrix()
	{
		if(m_Camera){
			m_Camera.ResetProjectionMatrix();
			var m = m_Camera.projectionMatrix;
			m.m11 *= m_CurrHeightScale;
			m_OffsetY = 1f-m_CurrHeightScale;
			m.m13 += m_OffsetY;
			m_Camera.projectionMatrix = m;

			if(_Bottom==null){
				CreateBottomCanvas();
			}
			_Bottom.sizeDelta = new Vector2(Screen.width,Screen.height*m_OffsetY);
		}
	}

	/// <summary>
	/// enable
	/// </summary>
	public static void Enable(){
		if(_Bottom) _Bottom.gameObject.SetActive(true);
		for(int i=0;i<_instances.Count;++i){
			CameraBannerAdjust ins = _instances[i];
			if(ins!=null && ins.cam!=null){
				ins.enabled=true;
				ins.UpdateCameraMatrix();
			}
		}
	}

	/// <summary>
	/// disable
	/// </summary>
	public static void Disable(){
		for(int i=0;i<_instances.Count;++i){
			CameraBannerAdjust ins = _instances[i];
			if(ins!=null && ins.cam!=null){
				ins.enabled=false;
				ins.cam.ResetProjectionMatrix();
			}
		}
		if(_Bottom) _Bottom.gameObject.SetActive(false); 
	}


	void CreateBottomCanvas(){
		GameObject go = new GameObject("__BottomCanvas__");
		go.hideFlags = HideFlags.HideInHierarchy;
		go.transform.localPosition = Vector3.zero;
		go.transform.localScale = Vector3.one;
		Canvas canvas = go.AddComponent<Canvas>();
		canvas.renderMode = RenderMode.ScreenSpaceOverlay;
		canvas.sortingOrder = 9999;
		go.AddComponent<GraphicRaycaster>();

		GameObject imgGo = new GameObject("__Img__");
		imgGo.transform.SetParent(go.transform);
		imgGo.transform.localPosition = Vector3.zero;
		imgGo.transform.localScale = Vector3.one;

		Image img = imgGo.AddComponent<Image>();
		img.color = Color.black;
		_Bottom = img.rectTransform;
		_Bottom.anchoredPosition = Vector2.zero;
		_Bottom.pivot = new Vector2(0.5f,0f);
		_Bottom.anchorMin = Vector2.zero;
		_Bottom.anchorMax = new Vector2(1f,0f);
		_Bottom.offsetMin = Vector2.zero;
	}

	void OnDestroy(){
		int len = _instances.Count;
		for(int i=0;i<len;++i){
			if(_instances[i] == null || _instances[i]==this){
				_instances.RemoveAt(i);
			}
		}
	}
}
